window.选择文章="";
window.shunxu=0;
window.datas;
function getuser() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
  //  var password1 = document.getElementById("password1").value;
    testing(username, password)
    //alert("username:"+username+"\n"+"password:"+password);
}

function zhuce() {
    var username = document.getElementById("user0").value;
    var password0 = document.getElementById("password0").value;
    var password1 = document.getElementById("password1").value;
    if(password0!=password1){
        alert("前后密码不一致");
        return 0;
    }
    var tmp = username && password0;
    if (tmp == "") {
            alert("请填写完整信息");
            return 0;
    }
    connectWebSocket();
    userObj={
        user:username,
        pwd:password0,
        action:'login',
    };
    sendMessage(userObj);
  //  var password1 = document.getElementById("password1").value;
   
    //alert("username:"+username+"\n"+"password:"+password);
}


function $(s){
    return document.getElementById(s);
}

$("zc").onclick=function(){
    zhuce();
}
function testing(username, password) {
    var tmp = username && password;
    if (tmp == "") {
            alert("请填写完整信息");
            return 0;
    }
    //alert("登录中")
    connectWebSocket();
    userObj={
        user:username,
        pwd:password,
        action:'login',
    };
    sendMessage(userObj);
}






// 清空消息
function clearMessage(id) {
//var msgDiv = document.getElementById('msg-div');
var msgDiv = document.getElementById(id);
// 获取 div 标签下的所有子节点
var pObjs = msgDiv.childNodes;
for (var i = pObjs.length - 1; i >= 0; i--) { // 一定要倒序，正序是删不干净的，可自行尝试
msgDiv.removeChild(pObjs[i]);
}
}

// 加载时连接websocket
window.onload = connectWebSocket;

//监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
window.onbeforeunload = function() {
closeWebSocket();
}

// 按键处理
window.onkeypress=function(e){
e = e|| window.event || arguments.callee.caller.arguments[0];
if(e && (e.keyCode==122 || e.keyCode==27)){
fullScreen(e);
//e.preventDefault();
// e.stopPropagation();
}
}

// 全屏处理
function fullScreen() {
var el = document.documentElement;
var isFullscreen = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
if (!isFullscreen) {//进入全屏,多重短路表达式
(el.requestFullscreen && el.requestFullscreen()) ||
(el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
(el.webkitRequestFullscreen && el.webkitRequestFullscreen()) || 
(el.msRequestFullscreen && el.msRequestFullscreen());
document.getElementById("showId").innerHTML = "退出全屏";	
} else {	//退出全屏,三目运算符
document.exitFullscreen ? document.exitFullscreen() :
document.mozCancelFullScreen ? document.mozCancelFullScreen() :
    document.webkitExitFullscreen ? document.webkitExitFullscreen() : '';
document.getElementById("showId").innerHTML = "全屏";		
}
}

//创建一个div
function creatdiv(msg, user){
var msgDiv = document.getElementById('msg-div');//获取对话框对话元素
var userDiv = document.createElement('div');//创建对话dom元素
var p= document.createElement('p');//存放文本内容
var img= document.createElement('img');//对话头像
//var br = document.createElement("br");//html中的元素
/*
    <div class="message other-message">
               <img class="avatar" src="img/1111111111.png" alt="avatar"/>
                <p>Hello!</p>

            </div>
            <div class="message user-message">
                <p>Hi there!</p>
                <img class="avatar" src="img/user头像.png" alt="avatar"/>
            </div>
*/
//userDiv.appendChild(br);
var now = new Date().Format("hh:mm:ss"); //当前时间
//userDiv.innerHTML = user + now;
if(user == "我") {
userDiv.className = "message user-message";
img.className = "avatar";
img.src="img/user头像.png";
} else if(user == "服务端 "||user == "服务端") {
    userDiv.className = "message other-message";
    img.className = "avatar";
    img.src="img/1111111111.png";
} else {
    userDiv.className = "message other-message";
    img.className = "avatar";
    img.src="img/1111111111.png";
}
p.innerHTML=msg;
if(user == "我") {
    userDiv.appendChild(p);
    userDiv.appendChild(img);
}else{
    userDiv.appendChild(img);
    userDiv.appendChild(p);
}
msgDiv.appendChild(userDiv);
}

function createyUserCenter(){
    /**
     *    <div class="dropdown">
                <select>
                    <option value="option1">Option 1</option>
                    <option value="option2">Option 2</option>
                    <option value="option3">Option 3</option>
                </select>
            </div>
     */

    var msgDiv = document.getElementById('msg-div');//获取对话框对话元素
    //var userDiv = document.createElement('div');//创建对话dom元素
    var button=document.createElement('button');
    var br = document.createElement('br');
    var input=document.createElement('input');
    var span1=document.createElement('span');
    span1.innerHTML="原 密  码 :";
    // 添加样式
    span1.style.fontFamily = "Arial, sans-serif";
    span1.style.fontSize = "14px";
    span1.style.color = "#333";
    span1.style.backgroundColor = "#f5f5f5";
    span1.style.border = "1px solid #ccc";
    span1.style.borderRadius = "3px";
    span1.style.padding = "5px";
    span1.style.boxShadow = "1px 1px 3px rgba(0, 0, 0, 0.2)";
    var span2=document.createElement('span');
    span2.innerHTML="新 密 码 :";
    span2.style.fontFamily = "Arial, sans-serif";
    span2.style.fontSize = "14px";
    span2.style.color = "#333";
    span2.style.backgroundColor = "#f5f5f5";
    span2.style.border = "1px solid #ccc";
    span2.style.borderRadius = "3px";
    span2.style.padding = "5px";
    span2.style.boxShadow = "1px 1px 3px rgba(0, 0, 0, 0.2)";
    input.id="beforePassword"
    var input2=document.createElement('input');
    input2.id="afterPassword"
    button.innerHTML = "提交";
    button.style.backgroundColor = "#f5f5f5";
    button.style.color = "#333";
    button.style.borderRadius = "5px";
    button.style.border = "none";
    button.style.boxShadow = "0 2px 4px rgba(0, 0, 0, 0.2)";
    button.id="changeUser";
    button.className='send-btn';
    document.body.appendChild(button);
    button.style.position = "absolute";
    button.style.top = "390px";
    button.style.left = "400px";
    // 添加样式
    input.style.fontFamily = "Arial, sans-serif";
    input.style.fontSize = "14px";
    input.style.color = "#333";
    input.style.backgroundColor = "#f5f5f5";
    input.style.border = "1px solid #ccc";
    input.style.borderRadius = "3px";
    input.style.padding = "5px";
    input.style.boxShadow = "1px 1px 3px rgba(0, 0, 0, 0.2)";
    input.maxLength = 16;
    input.style.width='350px';
    input2.style.width = '350px';
    input2.style.fontFamily = "Arial, sans-serif";
    input2.style.fontSize = "14px";
    input2.style.color = "#333";
    input2.style.backgroundColor = "#f5f5f5";
    input2.style.border = "1px solid #ccc";
    input2.style.borderRadius = "3px";
    input2.style.padding = "5px";
    input2.style.boxShadow = "1px 1px 3px rgba(0, 0, 0, 0.2)";
    input2.maxLength = 16;
    input2.style.width = '350px';
    var input3=document.createElement('input');
    input3.id="confirmPassword"
    input3.style.width = '350px';
    input3.style.fontFamily = "Arial, sans-serif";
    input3.style.fontSize = "14px";
    input3.style.color = "#333";
    input3.style.backgroundColor = "#f5f5f5";
    input3.style.border = "1px solid #ccc";
    input3.style.borderRadius = "3px";
    input3.style.padding = "5px";
    input3.style.boxShadow = "1px 1px 3px rgba(0, 0, 0, 0.2)";
    input3.maxLength = 16;
    input3.style.width = '350px';
    var span3=document.createElement('span');
    span3.innerHTML="新 密 码 :";
    span3.style.fontFamily = "Arial, sans-serif";
    span3.style.fontSize = "14px";
    span3.style.color = "#333";
    span3.style.backgroundColor = "#f5f5f5";
    span3.style.border = "1px solid #ccc";
    span3.style.borderRadius = "3px";
    span3.style.padding = "5px";
    span3.style.boxShadow = "1px 1px 3px rgba(0, 0, 0, 0.2)";
    span3.innerHTML="确认密码:";
    // 创建新的 <p> 元素
    var tip = document.createElement('p');
// 设置文本内容、
    tip.style.position='absolute'
    tip.textContent = '温馨提示：密码最长16位';

    tip.style.top = '388px';
    tip.style.left = '470px';
    tip.style.fontFamily = 'Arial';
    tip.style.fontSize = '14px';
    msgDiv.appendChild(tip);
    msgDiv.appendChild(span1);
    msgDiv.appendChild(input);
    msgDiv.appendChild(br);
    msgDiv.appendChild(span2);
    msgDiv.appendChild(input2);
    msgDiv.appendChild(document.createElement('br'));
    msgDiv.appendChild(span3);
    msgDiv.appendChild(input3);
    msgDiv.appendChild(document.createElement('br'));
    msgDiv.appendChild(button);
    $("changeUser").onclick=function(){
        var a=$("beforePassword").value;
        var b=$("afterPassword").value;
        var c=$("confirmPassword").value;
        if(b!=c){
            alert("前后密码不一致");
            return;
        }
        let r={
            action:"changeUser",
            before:a,
            after:b
        };
        ws_send(r);
        alert("已提交");
    }
}




function createSelect(data){
    /**
     *    <div class="dropdown">
                <select>
                    <option value="option1">Option 1</option>
                    <option value="option2">Option 2</option>
                    <option value="option3">Option 3</option>
                </select>
            </div>
     */
    data = JSON.parse(data);
    window.datas=data;
    /*
    // 遍历数组并访问其中的属性
for (let i = 0; i < data.length; i++) {
    
    console.log("Type: " + data[i].type);
    console.log("Content: ");
    for (let j = 0; j < data[i].content.length; j++) {
      console.log("- " + data[i].content[j]);
    }
  }
  */

    var msgDiv = document.getElementById('msg-div');//获取对话框对话元素
    var userDiv = document.createElement('div');//创建对话dom元素
    var br = document.createElement('br');
    var button=document.createElement('button');
    button.innerHTML="开始";
    button.id="begain_qing";
    button.className='send-btn';
    userDiv.className = "dropdown";


    clearMessage("fenlei");
    for(var i=0;i<data.length;i++){
    let select= document.createElement('select');//存放文本内容
    let option1= document.createElement('option');//选项
    option1.innerHTML=data[i].type;
    option1.value=data[i].type;
    select.appendChild(option1);
    
    $("fenlei").appendChild(option1);
    for(var j=0;j<data[i].content.length;j++){
    let option= document.createElement('option');//选择
    option.innerHTML=data[i].content[j];
    option.value=data[i].content[j];
    select.appendChild(option);
    }
    userDiv.appendChild(select);
}

   
    //msgDiv.appendChild(userDiv);
    //msgDiv.appendChild(br);
    //msgDiv.appendChild(button);
    //var displayDiv=document.createElement("div");
    //displayDiv.id="displaydiv";
    //msgDiv.appendChild(displayDiv);
    //监听事件
    var selectElements = document.querySelectorAll('#fenlei');
    selectElements.forEach(function(selectElement) {
    selectElement.addEventListener('change', function() {
    var selectedOption = this.options[this.selectedIndex];
    var selectedValue = selectedOption.value;
    window.选择分类=selectedValue;
   //$("displaydiv").innerHTML=window.选择文章;
    console.log(selectedValue);
    clearMessage("wenz");
    for(var i=0;i<window.datas.length;i++){
    //let select= document.createElement('select');//存放文本内容
    //let option1= document.createElement('option');//选择头像
    //option1.innerHTML=window.datas[i].type;
    //option1.value=window.datas[i].type;
    if(window.选择分类==window.datas[i].type){
    for(var j=0;j<window.datas[i].content.length;j++){
    let option= document.createElement('option');//选择
    option.innerHTML=window.datas[i].content[j];
    option.value=window.datas[i].content[j];
    $("wenz").appendChild(option);
    }
    //初始化为第一个值
    selectElement1 = document.getElementById("wenz");
    window.选择文章 = selectElement1.options[selectElement1.selectedIndex].value;
    // 手动触发一次change事件
    selectElement1.dispatchEvent(new Event('change'));
    break;
}
}


  });

 /* //手动触发chage事件
  触发一次“change”事件以记录第一个<select>元素的初始值
  var event = new Event('change');
  selectElement.dispatchEvent(event);
  */
  $("begain_qing").onclick=function(){
    res={
        action:window.actions,
        text: "文章"+"-"+window.选择文章+"-"+window.shunxu
    }
    ws_send(res);
    clearMessage("msg-div");
};
});
 //监听事件
 var selectElements = document.querySelectorAll('#wenz');
 selectElements.forEach(function(selectElement) {
 selectElement.addEventListener('change', function() {
 var selectedOption = this.options[this.selectedIndex];
 var selectedValue = selectedOption.value;
 window.选择文章=selectedValue;
$("displaydiv").innerHTML=window.选择文章;
 console.log(selectedValue);
});


$("begain_qing").onclick=function(){
    window.shunxu=0;
 res={
     action:window.actions,
     text: "文章"+"-"+window.选择文章+"-"+window.shunxu
 }
 ws_send(res);
 clearMessage("msg-div");
};
});



}


Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小时
"m+": this.getMinutes(), // 分
"s+": this.getSeconds(), // 秒
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
"S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
